unit HUPasswordDlg;

{$mode objfpc}{$H+}

//========================================================================================
//
// Unit : HUPasswordDlg.pas
//
// Description :
//
// Called By :
//
// Calls : HUtils
//
// Ver. : 1.0.0
//
// Date : 30 Sep 2015
//
//========================================================================================

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  Buttons,
  // Application units
  HUtils;

type

  { THUPasswordDialog }

  THUPasswordDialog = class(TForm)
    bbtOK: TBitBtn;
    bbtCancel: TBitBtn;
    edtPassword: TEdit;
    procedure bbtCancelClick(Sender: TObject);
    procedure bbtOKClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { private declarations }
    fPasswordChar : char;
    fPwd2Match : string;
    fNumberOfTries : byte;
    fPassword : string;
  public
    { public declarations }
//    function GetNumberOfTries : byte;
//    procedure SetNumberOfTries( Tries : byte);
  published
    property PasswordChar : char read fPasswordChar write fPasswordChar;
    property Pwd2Match : string read fPwd2Match write fPwd2Match;
    property NumberOfTries : byte read fNumberOfTries {write fNumberOfTries};
    property Password : string read fPassword;
  end;// THUPasswordDialog

const
  mrNoPasswordToMatch = mrAbort;
  mrNumberOfTriesExceeded = mrRetry;

var
  HUPasswordDialog: THUPasswordDialog;

implementation

{$R *.lfm}

const
    // Default Values
  cstrDefaultCaption = 'Enter the Password';
  cbytDefaultMinPwdLength = 7;
  cbytDefaultMaxPwdLength = 15;
  cchrDefaultPasswordChar = Char('*');
  cstrDefaultPwd2Match = '';
  cbytDefaultMinNumberOfTries = 1;
  cbytDefaultMaxNumberOfTries = 10;
  cbytDefaultNumberOfTries = 3;

  cstrPasswordMismatchMsg = 'Password Mismatch';
  cstrNoPasswordToMatch = 'No Password to Match';
  cstrTriesExceededMsg = 'Number of Tries Exceeded';

var
  vbytPasswordTryNumber : byte;

//========================================================================================
//          SUPPORT ROUTINES
//========================================================================================

//========================================================================================
//          PRIVATE ROUTINES
//========================================================================================

//========================================================================================
//          PUBLIC ROUTINES
//========================================================================================

//========================================================================================
//          PUBLISHED ROUTINES
//========================================================================================

//========================================================================================
//          PROPERTY ROUTINES
//========================================================================================
{function THUPasswordDialog.GetNumberOfTries : byte;
begin
  Result := fNumberOfTries;
end;// function THUPasswordDialog.GetNumberOfTries : byte; }

//----------------------------------------------------------------------------------------
{procedure THUPasswordDialog.SetNumberOfTries( Tries : byte);
begin
  If Tries < cbytDefaultMinNumberOfTries then
    Tries := cbytDefaultMinNumberOfTries;
  if Tries > cbytDefaultMaxNumberOfTries then
    Tries := cbytDefaultMaxNumberOfTries;
end;// procedure THUPasswordDialog.SetNumberOfTries( Tries : byte);}

//========================================================================================
//          MENU ROUTINES
//========================================================================================

//========== POPUP MENUS ===================================================================

//========================================================================================
//          TOOLBAR ROUTINES
//========================================================================================

//========================================================================================
//           STATUSBAR ROUTINES
//========================================================================================

//========================================================================================
//          FILE ROUTINES
//========================================================================================

//========================================================================================
//          COMMAND BUTTON ROUTINES
//========================================================================================
procedure THUPasswordDialog.bbtOKClick(Sender: TObject);
begin

  vbytPasswordTryNumber := vbytPasswordTryNumber + 1;

  if edtPassword.Text <> Pwd2Match then
  begin
    showmessage(cstrPasswordMismatchMsg);

    if vbytPasswordTryNumber >= fNumberOfTries then
    begin
      showmessage(cstrTriesExceededMsg);
      ModalResult := mrNumberOfTriesExceeded;
      Exit;
    end;// if vbytPasswordTryNumber >= fNumberOfTries

    ModalResult := mrNone;
    edtPassword.SetFocus;

  end;// if edtPassword.Text <> Pwd2Match

end;// procedure THUPasswordDialog.bbtOKClick(Sender: TObject);

//========================================================================================
procedure THUPasswordDialog.bbtCancelClick(Sender: TObject);
begin

end;// procedure THUPasswordDialog.bbtCancelClick(Sender: TObject);

//========================================================================================

//========================================================================================
//          CONTROL ROUTINES
//========================================================================================

//========================================================================================
//          DATABASE ROUTINES
//========================================================================================

//========================================================================================
//          FORM ROUTINES
//========================================================================================
procedure THUPasswordDialog.FormCreate(Sender: TObject);
begin

  HUPasswordDialog.Caption := cstrDefaultCaption;

  edtPassword.MaxLength := cbytDefaultMaxPwdLength;
  fPasswordChar := cchrDefaultPasswordChar;
  fPwd2Match := 'Da2yiiaa';
  fNumberOfTries := cbytDefaultNumberOfTries;

end;// procedure THUPasswordDialog.FormCreate

//========================================================================================
procedure THUPasswordDialog.FormShow(Sender: TObject);
begin

  vbytPasswordTryNumber := 0;
  edtPassword.PasswordChar:=fPasswordChar;

  if Pwd2Match = '' then
  begin
    showmessage(cstrNoPasswordToMatch);
    ModalResult := mrNoPasswordToMatch;
    Close;
  end;// if Pwd2Match = cstrDefaultPwd2Match

end;// procedure THUPasswordDialog.FormShow


//========================================================================================

end.// unit HUPasswordDlg

